<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Member extends MY_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('member_model');
	}
	
	
	public function login(){
		if($this->member_model->is_login()){
			redirect(site_url('c=model&m=index'));
		}
		$this->load->view('member/login',$this->_data);
	}
	
	public function login_do(){

	    $uname = $this->p['uname'];
		$upass = $this->p['upass'];
		
		//登录
		if($this->member_model->login($uname,$upass)){
			ejd('succ-href', '', site_url('c=model&m=index'));
		}else{
			ejd('fail', '错误的用户名或密码！');
		}
	}
	
	function loginout(){
		session_destroy();
		redirect(site_url('c=member&m=login'));
	}

	function regist(){
	    if($this->is_login){
	        redirect(site_url());
	    }
	    if(!empty($this->p['invitor'])){
		    $this->_data['invitor'] = $this->p['invitor'];
	    }
		$this->load->view('member/regist',$this->_data);
	}
	
	function follow(){
		
		//登录检测
		$this->check_ajax_login();
		
		//数据校验
		$id = $this->p['id'];
		if(empty($id)) die;
		if($this->loginer->id == $id) ejd('unlogin');
		if(!$this->cm->in('member',array('id'=>$id))) die;

		$cond = array('uid'=>$this->loginer->id,'fid'=>$id);
		if($this->cm->in('follow',$cond)){
			$this->cm->delete('follow',$cond);
			echo json_encode(array('status'=>'unfollowed'));
		}else{
			$this->cm->insert('follow',$cond);
			echo json_encode(array('status'=>'followed'));
		}
		exit;
	}
	
	function ajax_login(){
		echo $this->load->view('member/ajax_login',$this->_data,true);
	}
	
	public function ajax_login_do(){

		$email = $this->p['email'];
		$upass = $this->p['upass'];
		
		//用户数据验证
		if(!$this->form_validation->required($email)) die;
		if(!$this->form_validation->required($upass)) die;
		
		//登录
		if($this->member_model->login($email,$upass)){
			ejd('', '', 'reload');
		}else{
			ejd('fail', '错误的用户名或密码！');
		}
	}
	
	
	
	
}

